package com.my.chapter9;

public class practice11 {
    public static void main(String[] args) {
        SwapCharacters s = new SwapCharacters();
        SwapCharactersAdapter sa = new SwapCharactersAdapter(s);
        Apply.process(sa,"abcde");
    }
}

class SwapCharacters {
    public String process(String input) {
        char[] charArray = input.toCharArray();
        int length = charArray.length;
        if(length%2!=0){
            length--;
        }
        for (int i = 0; i < length; i += 2) {
            char temp = charArray[i];
            charArray[i] = charArray[i + 1];
            charArray[i + 1] = temp;
        }

        return new String(charArray);
    }

    public String name() {
        return getClass().getSimpleName();
    }
}

interface Processor {
    String name();
    Object process(Object input);
}

class SwapCharactersAdapter implements Processor {
    SwapCharacters swapCharacters;

    public SwapCharactersAdapter(SwapCharacters swapCharacters) {
        this.swapCharacters = swapCharacters;
    }

    @Override
    public String name() {
        return swapCharacters.name();
    }

    @Override
    public Object process(Object input) {
        return swapCharacters.process((String) input);
    }
}

class Apply {
    public static void process(Processor p, Object s) {
        System.out.println("Using Processor " + p.name());
        System.out.println(p.process(s));
    }
}




